﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.App;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.App;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// app版本控制
    /// </summary>
    [Route("/api/v1/sysuser/appVersion")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class AppVersionController : ControllerBase
    {
        private readonly SystemUserApplicationContext applicationContext;
        private readonly IMapper mapper;
        private readonly AppVersionService appVersionService;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="applicationContext">上下文</param>
        /// <param name="mapper">mapper映射</param>
        /// <param name="AppVersionService">主业务service</param>
        public AppVersionController(SystemUserApplicationContext applicationContext, IMapper mapper, AppVersionService appVersionService)
        {
            this.applicationContext = applicationContext;
            this.mapper = mapper;
            this.appVersionService = appVersionService;
        }

        /***进入模版管理可以修改模版***/

        /// <summary>
        /// 创建app版本控制信息
        /// </summary>
        /// <param name="form">表单数据</param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("appVersion:add")]
        public ApiResult Add([FromBody] AppVersionEditForm form)
        {
            AppVersion entity = new();
            mapper.Map(form, entity);
            entity.CreateTime = DateTime.Now;
            appVersionService.Create(entity);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// app版本控制信息表单数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        [PermissionCheck("appVersion:details")]
        public ApiResult<AppVersionEditForm> GetRole(int id)
        {
            var data = appVersionService.Get(id);
            if (data == null)
                return ApiResult<AppVersionEditForm>.Failure("app版本控制信息数据不存在");
            return ApiResult<AppVersionEditForm>.Success(data.AsForm());
        }

        /// <summary>
        /// 修改app版本控制信息
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="form">表单信息</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("appVersion:edit")]
        public ApiResult Update(int id, [FromBody] AppVersionEditForm form)
        {
            var data = appVersionService.Get(id);
            if (data == null) return ApiResult.Failure("app版本控制信息不存在");
            mapper.Map(form, data);
            appVersionService.Update(data);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 删除app版本控制信息
        /// </summary>
        /// <param name="ids">id集合，多个用,隔开</param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("appVersion:delete")]
        public ApiResult Delete(string ids)
        {
            appVersionService.Deleted(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }
    }
}